# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Leonardo Arena <larena@alpinelinux.org>
pkgname=lxd
pkgver=5.0.0 # only LTS releases
pkgrel=0
pkgdesc="A container hypervisor and a new user experience for LXC - 'LTS' release channel"
url="https://linuxcontainers.org/lxd/"
arch="all"
license="Apache-2.0"
depends="acl
	attr
	ca-certificates
	cgmanager
	dbus
	dnsmasq
	lxc
	ip6tables
	iproute2
	netcat-openbsd
	rsync
	squashfs-tools
	shadow-uidmap
	tar
	xz
	"
makedepends="acl-dev
	autoconf
	automake
	dqlite-dev
	eudev-dev
	gettext-dev
	go
	intltool
	libcap-dev
	libtool
	libuv-dev
	linux-headers
	lxc-dev
	lz4-dev
	raft-dev
	rsync
	sqlite-dev
	tcl-dev
	"
subpackages="$pkgname-scripts:scripts
	$pkgname-bash-completion:bashcomp:noarch
	$pkgname-openrc
	$pkgname-client
	"
provides=$pkgname-lts=$pkgver-r$pkgrel
install="$pkgname.pre-install"
options="!check" # FIXME: several tests failing
source="https://linuxcontainers.org/downloads/$pkgname/$pkgname-$pkgver.tar.gz
	$pkgname.confd
	$pkgname.initd

	add-missing-includes.patch
	0001-no-go-get.patch
	"

_project="github.com/lxc/lxd"

build() {
	export GOPATH="$builddir/_dist"
	export CGO_CFLAGS="-I/usr/include/raft -I/usr/include/dqlite"
	export CGO_LDFLAGS="$LDFLAGS -lintl"
	export CGO_LDFLAGS_ALLOW="(-Wl,-wrap,pthread_create)|(-Wl,-z,now)"
	export GOFLAGS="$GOFLAGS -buildmode=pie -trimpath"
	export GO111MODULE=on

	make

	for bin in lxc fuidshift lxc-to-lxd lxd-benchmark lxd-agent; do
		go install -v -x $_project/$bin
	done
}

package() {
	cd $GOPATH
	install -Dm755 bin/$pkgname "$pkgdir"/usr/sbin/$pkgname
	install -Dm755 bin/lxc "$pkgdir"/usr/bin/lxc

	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/lxd
	install -Dm644 "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/lxd
}

bashcomp() {
	depends="bash"
	pkgdesc="Bash completions for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"
	provides=$pkgname-lts-bash-completion=$pkgver-r$pkgrel

	cd "$builddir"
	mkdir -p "$subpkgdir"/usr/share/bash-completion/completions
	cp scripts/bash/lxd-client "$subpkgdir"/usr/share/bash-completion/completions/lxd-client
}

scripts() {
	pkgdesc="LXD scripts"
	depends="$pkgname py3-lxc"
	provides=$pkgname-lts-scripts=$pkgver-r$pkgrel

	export GOPATH="$builddir/_dist"
	cd $GOPATH

	for tool in fuidshift lxc-to-lxd lxd-benchmark lxd-agent; do
		install -Dm755 bin/$tool "$subpkgdir"/usr/bin/$tool
	done
	install -Dm755 ../scripts/empty-lxd.sh "$subpkgdir"/usr/bin/empty-lxd.sh
}

openrc() {
	provides=$pkgname-lts-openrc=$pkgver-r$pkgrel
	default_openrc
}

client() {
	pkgdesc="LXD CLI client"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/lxc "$subpkgdir"/usr/bin
}

check() {
	LXD_OFFLINE=true make check
}

sha512sums="
e74c14a2642ee4ea738629b1da9c4b416f3bdbb26a49c8c920eb0e5e0467236fd610276541af1d481d6b069093cb1c35c705008652ff2cd55d2d15e9b7433696  lxd-5.0.0.tar.gz
bc32c71f2ce10f508433e1e4651c08c18e8a17e9419a7ce391c0f127fc7cf378c665178926b35eae8813e290d9c5eab3ceb605679fd32efdf2cf98a57cee4127  lxd.confd
32a10201441e65bc342c9feffce9e2cd2977db4d8fe9c4d2d32530ff787a31088d97cb5a76a27cd7e1bcdb0ed3c3363106ff0ebfa8043b978ad9ec7262f4c088  lxd.initd
1e3a33a1e59ff202b556dd20596abf562d6af652b50ac6ce85d762043e765c0b5f91676735319132ed68d543c7857796e8a1aa9a642ae892e4c1311c727dbd2a  add-missing-includes.patch
1f4ea3c2e6ce73604407d26adf45e2f9bd6d5e94f6d914d278ce2c82c38cc29b65c063962c7e2f0ae996828bf2de9fd61177fe979ad7e5865766c1cd1103c2cb  0001-no-go-get.patch
"
